#if DEBUG_VERSION
if (!dafos) {
  throw new Error("Dafos namespace not found!");
}
#endif

#if SNIPPETSTAB

dafos.onSnippetsTabActivated = function() {
  if (dafos.snippetstab == null) {
    dafos.snippetstab = new dafos.SnippetsTab(document.getElementById("snippetstab"), dafos.ui);
  }
  TRACK("tab/snippets");
  RESIZE();
};

/**
 * Constructor
 */
dafos.SnippetsTab = function(parent, ui) {
  LOG("[SnippetsTab]");
  this.appengine = new dafos.AppEngine();
  var callbacks = {
    onselected : dafos.bind(this.onSelected, this)
  };
  this.gadgetui = ui;
  this.ui = new dafos.SnippetsTabUI(parent, callbacks);
  this.gadgetui.showLoading("snippets"); 
  this.appengine.getGlobalSnippets(dafos.bind(this.onSnippetsLoaded, this));
};

dafos.SnippetsTab.prototype.onSnippetsLoaded = function(response) {
  LOG("SNIPPETS", response);
  this.gadgetui.hideLoading("snippets");
  if (response.error) {
    this.gadgetui.setError("Problem loading samples: " + response.error);
    return;
  }
  this.gadgetui.setMessage(response.message);
  if (response.status == "success") {
    for (var i = 0, snippet; snippet = response.payload[i]; i++) {
      this.ui.renderEntry(snippet);
    }
  }
};

dafos.SnippetsTab.prototype.onSelected = function(id) {
  this.gadgetui.showLoading("sample"); 
  this.appengine.getSample(id, dafos.bind(this.onSampleLoaded, this));
};

dafos.SnippetsTab.prototype.onSampleLoaded = function(response) {
  this.gadgetui.hideLoading("sample"); 
  if (response.error) {
    this.gadgetui.setError(response.error);
    return;
  }
  this.gadgetui.setMessage(response.message);
  if (response.status == "success") {
    var tablist = dafos.tabs.getTabs();
    for (var i = 0, tab; tab = tablist[i]; i++) {
      if (tab.getName().toLowerCase() == "code") {
        dafos.tabs.setSelectedTab(i);
        break;
      }
    }
    dafos.codetab.setCodeToSnippet(response.payload.text);
  }
};


/**
 * Constructor
 */
dafos.SnippetsTabUI = function(parent, callbacks) {
  LOG("[SnippetsTabUI]");
  this.dom_parent_ = parent;
  this.callbacks_ = callbacks;
  this.render();
};

dafos.SnippetsTabUI.prototype.render = function() {
  e34.$("snippetstab").removeClass("hidden");
  this.entry_counter = 0;  
  this.entry_template = e34.$("snippetstab-entries-item-template");
  this.entry_parent = e34.$(this.entry_template.getParentElement());
  this.entry_template.removeFromParent();
};

dafos.SnippetsTabUI.prototype.renderEntry = function(entry) {
  LOG("entry:", entry);
  this.entry_counter++;
  
  var entry_row = e34.renderTemplate(this.entry_template, { 
    id: this.entry_counter,
    title: entry.name,
    time: dafos.timeToPrettyString(Date.parse(entry.updated)),
    summary: entry.description,
    code: entry.text
  });
  var numlines = entry.text.split("\n").length + 2;
  var ems = Math.round(numlines * 1.125);
  entry_row.$C("code")[0].setStyleHeight(ems + "em");
  entry_row.$C("load-link")[0].addOnClickHandler(
      dafos.bind(this.onSelected, this, entry.id));
  
  this.entry_parent.appendChildren(entry_row);

  gadgets.window.adjustHeight();
};

dafos.SnippetsTabUI.prototype.onSelected = function(id) {
  TRACK("action/loadsnippet/" + id);
  this.callbacks_.onselected(id);
};


#endif

